<?php
class Exts_Security_ExtUserAdapter {
	protected $_userModel = "Application_Model_Users";
	protected $_userModelInstance = NULL;
	protected $_usersRow = array(); 

	public function init () {}
	protected function _getModelInstance () {
		if (!$this->_userModelInstance) {
			$model = $this->_userModel;
			$this->_userModelInstance = new $model();	 
		}
		return $this->_userModelInstance;
	}
	
	public function getUser ($user) {
		if (!array_key_exists($user->username,$this->_usersRow)){
			$this->_usersRow[$user->username] = $this->_getModelInstance()->getUserByUsername($user->username);
		}
		return $this->_usersRow[$user->username];
	}
	
	public function validateUser ($user) {
		$trueUser = $this->getUser($user);
		if($trueUser){
			return  $this->_getModelInstance()->isValidUser($trueUser, $user->password);
		}
		else {
			return false;
		}
	}
}